/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamus.manager;

import com.kamus.Constants;
import com.kamus.DB;
import com.kamus.model.FileDb;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileDbMgr extends BaseMgr {

    private static FileDbMgr instance;
    public static final int TYPE_GI = 1;
    public static final int TYPE_IG = 2;

    public FileDbMgr() {
        connection = getConnection();
    }

    public static FileDbMgr getInstance() {
        if (instance == null) {
            instance = new FileDbMgr();
        }
        return instance;
    }

    public int insertFile(FileDb fileDb) {
        int id = -1;
        cekConnection();
        try {
            psmnt = connection.prepareStatement("insert into FILE_DB(NAMA,TANGGAL,TYPE) values(?,?,?)");
            psmnt.setString(1, fileDb.getNama());
            psmnt.setDate(2, new java.sql.Date(fileDb.getTanggal().getTime()));
            psmnt.setString(3, fileDb.getType());
            id = psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public void deleteFile(int id) {
        cekConnection();
        String fileName = getFileDbById(id).getNama();
        try {
            psmnt = connection.prepareStatement("delete from FILE_DB where ID=?");
            psmnt.setInt(1, id);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        File f = new File(Constants.PATH + fileName + ".dat");
        f.delete();
    }

    public void deleteFile(FileDb fileDb) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("delete from FILE_DB where ID=?");
            psmnt.setInt(1, fileDb.getId());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        File f = new File(Constants.PATH + fileDb.getNama() + ".dat");
        f.delete();
    }

    public ArrayList<FileDb> getFiles(String order) {
        cekConnection();
        ArrayList<FileDb> list = new ArrayList<FileDb>();
        FileDb fileDb;
        try {
            psmnt = connection.prepareStatement("select * from FILE_DB order by " + order);
            rs = psmnt.executeQuery();
            while (rs.next()) {
                fileDb = new FileDb();
                fileDb.setId(rs.getInt("id"));
                fileDb.setNama(rs.getString("nama"));
                fileDb.setTanggal(rs.getDate("tanggal"));
                fileDb.setType(rs.getString("type"));
                list.add(fileDb);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public ArrayList<FileDb> getFiles(int type,String order) {
        cekConnection();
        ArrayList<FileDb> list = new ArrayList<FileDb>();
        FileDb fileDb;
        String strType = "";
        if (type == TYPE_GI) {
            strType = "gi";
        } else if (type == TYPE_IG) {
            strType = "ig";
        }
        try {
            psmnt = connection.prepareStatement("select * from FILE_DB where type=? order by " + order);
            psmnt.setString(1, strType);
            rs = psmnt.executeQuery();
            while (rs.next()) {
                fileDb = new FileDb();
                fileDb.setId(rs.getInt("id"));
                fileDb.setNama(rs.getString("nama"));
                fileDb.setTanggal(rs.getDate("tanggal"));
                fileDb.setType(rs.getString("type"));
                list.add(fileDb);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public void updateFile(FileDb fileDb) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("update FILE_DB set NAMA=?, DATE=?, TYPE=? where ID= ?");
            psmnt.setString(1, fileDb.getNama());
            psmnt.setDate(2, new java.sql.Date(fileDb.getTanggal().getTime()));
            psmnt.setString(3, fileDb.getType());
            psmnt.setInt(4, fileDb.getId());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public FileDb getUsed(int type) {
        FileDb fileDb = null;
        String strType = "";
        if (type == TYPE_GI) {
            strType = "gi";
        } else if (type == TYPE_IG) {
            strType = "ig";
        }
        try {
            psmnt = connection.prepareStatement("select * from FILE_DB where TYPE = ? and USED='Y'");
            psmnt.setString(1, strType);
            rs = psmnt.executeQuery();
            while (rs.next()) {
                fileDb = new FileDb();
                fileDb.setId(rs.getInt("id"));
                fileDb.setNama(rs.getString("nama"));
                fileDb.setTanggal(rs.getDate("tanggal"));
                fileDb.setType(rs.getString("type"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fileDb;
    }

    public FileDb getFileDbById(int id) {
        FileDb fileDb = null;
        try {
            psmnt = connection.prepareStatement("select * from FILE_DB where ID = ? ");
            psmnt.setInt(1, id);
            rs = psmnt.executeQuery();
            while (rs.next()) {
                fileDb = new FileDb();
                fileDb.setId(rs.getInt("id"));
                fileDb.setNama(rs.getString("nama"));
                fileDb.setTanggal(rs.getDate("tanggal"));
                fileDb.setType(rs.getString("type"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fileDb;
    }
}
